{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0acd2fa1-95a6-4c8a-8ff0-e4e224c7b041",
   "metadata": {},
   "outputs": [],
   "source": [
    "#类和对象\n",
    "class Document():\n",
    "    def __init__(self,title,author,context):\n",
    "        self.title = title\n",
    "        self.author = author\n",
    "        self.__context = context\n",
    "    def get_context_length(self):\n",
    "        return len(self.__context)\n",
    "    def intercept_context(self,context):\n",
    "        self.__context = self.__context[:context]\n",
    "harry_potter_book = Document(\"Harry Potter\",\"J.K.Rowling\",\"...Forever Do not believe any thing is capable of thinking independently...\")\n",
    "print(harry_potter_book.title)\n",
    "print(harry_potter_book.author)\n",
    "print(harry_potter_book.get_context_length())\n",
    "harry_potter_book.intercept_context(10)\n",
    "print(harry_potter_book.__context)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "24dbe871",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Document():\n",
    "    WELCOME_STR = 'Welcome! The context for this book is {}.'\n",
    "    def __init__(self,title,author,context):\n",
    "        self.title = title\n",
    "        self.author = author\n",
    "        self.__context = context\n",
    "    # 类函数\n",
    "    @classmethod\n",
    "    def create_empty_book(cls,title,author):\n",
    "        return cls(title = title,author = author,context ='nothing')\n",
    "    # 成员函数\n",
    "    def get_context_length(self):\n",
    "        return len(self.__context)\n",
    "    # 静态函数\n",
    "    @staticmethod\n",
    "    def get_welcome(context):\n",
    "        return Document.WELCOME_STR.format(context)\n",
    "empty_book = Document.create_empty_book('What Every Man Thinks About Apart from Sex', 'Professor Sheridan Simove')\n",
    "print(empty_book.get_context_length())\n",
    "print(empty_book.get_welcome('indeed nothing'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec7a7597",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 继承\n",
    "class Entity():\n",
    "    def __init__(self, object_type):\n",
    "        print('parent class init callde')\n",
    "        self.object_type = object_type\n",
    "    def get_context_length(self):\n",
    "        raise Exception('get_context_length not implemented')\n",
    "    def print_title(self):\n",
    "        print('-------')\n",
    "        print(self.title)\n",
    "class Document(Entity):\n",
    "    def __init__(self,title,author,context):\n",
    "        print(\"Document class init called\")\n",
    "        Entity.__init__(self,'Document')\n",
    "        self.title = title\n",
    "        self.author = author\n",
    "        self.__context = context\n",
    "    def get_context_length(self):\n",
    "        return len(self.__context)\n",
    "class Video(Entity):\n",
    "    def __init__(self,title,author,video_length):\n",
    "        print(\"Video class init called\")\n",
    "        Entity.__init__(self,'Video')\n",
    "        self.title = title\n",
    "        self.author = author\n",
    "        self.__video_length = video_length\n",
    "    def get_context_length(self):\n",
    "        return self.__video_length\n",
    "harry_potter_book = Document('Harry Potter Book','J.K.Rowling','... Forever Do not believe any thing is capable of thinking independently ...')\n",
    "harry_potter_movie = Video('Harry Potter Movie','J.K.Rowling',120)\n",
    "print('---------------------')\n",
    "print(harry_potter_book.object_type)\n",
    "print(harry_potter_movie.object_type)\n",
    "print(\"------------------------\")\n",
    "print(harry_potter_book.print_title())\n",
    "#print(harry_potter_movie.print_title())\n",
    "print('------------------')\n",
    "print(harry_potter_book.get_context_length())\n",
    "print(harry_potter_movie.get_context_length())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff583a6b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from abc import ABCMeta,abstractmethod\n",
    "class Entity(metaclass=ABCMeta):\n",
    "    @abstractmethod\n",
    "    def get_title(self):\n",
    "        pass\n",
    "    @abstractmethod\n",
    "    def set_title(self,title):\n",
    "        pass\n",
    "class Document(Entity):\n",
    "    def get_title(self):\n",
    "        return self.title\n",
    "    def set_title(self, title):\n",
    "        self.title = title\n",
    "document = Document()\n",
    "document.set_title('Harry Potter')\n",
    "print(document.get_title())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e83500d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "#思考题\n",
    "#一个基类的初始化函数可能被调用两次。在一般的工程中，这显然不是我们所希望的。\n",
    "#正确的做法应该是使用 super 来召唤父类的构造函数，而且 python 使用一种叫做方法解析顺序的算法（具体实现算法叫做 C3），来保证一个类只会被初始化一次。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fc1f3181",
   "metadata": {},
   "outputs": [],
   "source": [
    "# A ->B\n",
    "# A ->C\n",
    "# B,C -> D\n",
    "class A():\n",
    "    def __init__(self):\n",
    "        print('enter A')\n",
    "        print('leave A')\n",
    "class B(A):\n",
    "    def __init__(self):\n",
    "        print('enter B')\n",
    "        super().__init__()\n",
    "        print('leave B')\n",
    "class C(A):\n",
    "    def __init__(self):\n",
    "        print('enter C')\n",
    "        super().__init__()\n",
    "        print('leave C')\n",
    "class D(B,C):\n",
    "    def __init__(self):\n",
    "        print('enter D')\n",
    "        super().__init__()\n",
    "        print('leave D')\n",
    "D()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a140abea",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd  \n",
    "\n",
    "# 创建示例 DataFrame  \n",
    "data = {  \n",
    "    'column_name': [  \n",
    "        'DYF3+2-YFH-A-1439',  \n",
    "        'Another String',  \n",
    "        'DYF3+2-YFH-A-1439 with more text',  \n",
    "        'Something else',  \n",
    "        'DYF3-1-YFH-A-1234'  \n",
    "    ]  \n",
    "}  \n",
    "\n",
    "df = pd.DataFrame(data)  \n",
    "\n",
    "# 使用 str.contains 查找包含特定字符串的行  \n",
    "target_string = 'DYF3+2-YFH-A-1439'  \n",
    "\n",
    "# 过滤 DataFrame  \n",
    "filtered_df = df[df['column_name'].str.contains(target_string, na=False,regex=False)]  \n",
    "\n",
    "# 打印结果  \n",
    "print(filtered_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "673d4e35-62a7-4d97-a38e-510d808540a8",
   "metadata": {},
   "outputs": [],
   "source": [
    "#混入\n",
    "class WaterMixin(object):\n",
    "    def __init__(self):\n",
    "        self.temperature = None\n",
    "        self.volume = 0\n",
    "    def addWater(self,nums,temp):\n",
    "        self.volume = nums\n",
    "        if temp == \"cold\":\n",
    "            self.temperature = \"cold\"\n",
    "        elif temp == \"hot\":\n",
    "            self.temperature = \"hot\"\n",
    "        return f\"add {self.temperature} water {self.volume} ML\"\n",
    "class CoffeeMixin(object):\n",
    "    def __init__(self) -> None:\n",
    "        self.numbers = 0\n",
    "    def addCoffee(self,nums):\n",
    "        self.numbers = nums\n",
    "        return f\"add coffee {self.numbers} part\"\n",
    "class MilkMixin(object):\n",
    "    def __init__(self) -> None:\n",
    "        self.temperature = None\n",
    "        self.volume = 0\n",
    "    def addMilk(self,nums,temp):\n",
    "        self.volume = nums\n",
    "        if temp == \"cold\":\n",
    "            self.temperature = \"cold\"\n",
    "        if temp == \"hot\":\n",
    "            self.temperature = \"hot\"\n",
    "        return f\"add {self.temperature} milk {self.volume} ML\"\n",
    "class Coffee(WaterMixin,CoffeeMixin,MilkMixin):\n",
    "    def __init__(self,water=-1,water_temp=\"cold\",milk=-1,milk_temp=\"cold\",coffee=1):\n",
    "        self.water = water\n",
    "        self.water_temp = water_temp\n",
    "        self.milk = milk\n",
    "        self.milk_temp = milk_temp\n",
    "        self.coffee = coffee\n",
    "        self.prescription = []\n",
    "        if int(self.water) >0:\n",
    "            self.prescription.append(super().addWater(self.water,self.water_temp))\n",
    "        if int(self.milk) > 0:\n",
    "            self.prescription.append(super().addMilk(self.milk,self.milk_temp))\n",
    "        if int(self.coffee) > 0:\n",
    "            self.prescription.append(super().addCoffee(self.coffee))\n",
    "    def showPrescription(self):\n",
    "        print(self.prescription)\n",
    "        for i in self.prescription:\n",
    "            print(i)\n",
    "americano = Coffee(water=2,water_temp=\"hot\",milk=150,milk_temp=\"hot\")\n",
    "americano.showPrescription()\n",
    "print('--------------------')\n",
    "print(WaterMixin.__dict__.keys())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
